//index.js
//获取应用实例
const app = getApp()
const util = require("../../utils/util.js");
var QQMapWX = require('../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
    key: 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'
});
var hotMessageNum = 4;
Page({
    data: {
        menuIndex: 0,
        // cityname: app.globalData.extConfig.cityname,
        bottomHeight: 0,
        showtk: false,
        indexCue: true,
        city: "深圳",
        citycode: 'sz',
        hotList: [],
        toView: '',
        infolist: [],
        wendaData: [],
        channelFixed: "",
        scrollflag: false,
        topNum: "150",
        showloading: true,
        navtop: 44,
        inputHeight: 30,
        // scrollArr:[],
        // topShow:true,
        // heightArr: ["857", "1528", "2232", "2911"]
        tools: [{
            name: "查办事",
            img: "/images/index/banshi.png",
            path: "/pages/tools/allServices/allServices?menuIndex=0"
        }, {
            name: "查交通",
            img: "/images/index/jiaotong.png",
            path: "/pages/tools/allServices/allServices?menuIndex=2"
        }, {
            name: "查活动",
            img: "/images/index/huodong.png",
            path: "/pages/tools/hdcx/hdcx"
        }, {
            name: "常用查询",
            img: "/images/index/changyong.png",
            path: "/pages/tools/commonQuery/commonQuery"
        }],

        hotMessage: []
    },

    onLoad: function (opt) {
        if (wx.getStorageSync("indexCue")) {
            this.setData({
                indexCue: false,
            })
        }
        this.setData({
            citycode: opt.city ? opt.city : (wx.getStorageSync("citycode") ? wx.getStorageSync("citycode") : this.data.citycode),
            showTap: app.globalData.showTap
        })
        wx.setStorageSync('citycode', this.data.citycode)
        util.getUrl(app.globalData.url).then((res) => {
            util.cityList().then((res) => {
                let allcity = res.allcity;
                if (!opt.city) {
                    qqmapsdk.reverseGeocoder({
                        success: (res) => {
                            let city = res.result.address_component.city;
                            let citydata;
                            if (allcity && Array.isArray(allcity))
                                citydata = allcity.filter(v => {
                                    if (city != undefined) {
                                        if (city.indexOf(v.cityname) != -1) {
                                            return v;
                                        }
                                    }
                                });
                            if (city && !city.includes(wx.getStorageSync("city"))) {
                                if (citydata && citydata[0]) {
                                    let citycode = citydata[0] ? citydata[0].citycode : '';
                                    wx.showModal({
                                        title: '温馨提醒',
                                        content: '当前城市与您定位城市不一样，是否切换城市？',
                                        success: res => {
                                            if (res.confirm) {
                                                wx.reLaunch({
                                                    url: '/pages/index/index?city=' + citycode,
                                                })
                                            }
                                        }
                                    })
                                } else {
                                    return;
                                }
                            }
                        },
                    });
                }

                let citydatas;
                if (allcity && Array.isArray(allcity)) citydatas = allcity.filter(v => v.citycode == this.data.citycode);
                if (Array.isArray(citydatas) && citydatas.length)
                    this.setData({
                        city: Array.isArray(citydatas) ? citydatas[0].cityname : this.data.city,
                    })
                wx.setStorageSync('city', this.data.city)
                this.setData({
                    showloading: true,
                })
                this.getBannerData();
                this.getInfoData();
                this.getwendaData();
                this.getHotztData();
                this.getHotMessage(hotMessageNum);
            })
            // this.getCityConfig();
        });

        //获取胶囊位置信息
        if (wx.getMenuButtonBoundingClientRect) {
            let menuButtonObject = wx.getMenuButtonBoundingClientRect();
            if (menuButtonObject)
                this.setData({
                    navtop: menuButtonObject.top,
                    inputHeight: menuButtonObject.height,
                })
        }
        // 获取屏幕高度
        wx.getSystemInfo({
            success: (res) => {
                var clientHeight = res.windowHeight,
                    clientWidth = res.windowWidth,
                    rpxR = 750 / clientWidth;
                var calc = clientHeight * rpxR;
                this.setData({
                    winHeight: calc
                });
            }
        });
    },

    // 获取热点资讯

    getHotMessage(num) {
        util.request('phb.php', {
            citycode: this.data.citycode,
        }, "GET").then(res => {
            if (res.data && Array.isArray(res.data)) {
                if (res.data.length > 0) {
                    this.setData({
                        hotMessage: res.data.splice(0, num),
                    });
                }
            }
        });
    },

    //获取热门办事
    getHotztData() {
        let toolData = {
            action: "tools",
            city: wx.getStorageSync("city") ? wx.getStorageSync("city") : this.data.city,
            keywords: "",
            version: app.globalData.version,
            type: "热门办事",
        };
        util.request('service.php', toolData, "POST" ).then(res => {
            if (res.data.data) {
                res.data.data = res.data.data.slice(0, 8)
                let hotbsyData = [];
                hotbsyData = res.data.data;
                this.setData({
                    hotbsyData
                })
            }
            // this.showView();
        })
    },

    // 查询点击
    toolsBtn(e) {
        let path = e.currentTarget.dataset.path;
        if (path) {
            wx.navigateTo({
                url: path,
            })
        }
    },

    // 首页swiper-banner
    getBannerData() {
        let bannerData = {
            action: 'lunbo',
            city: wx.getStorageSync("city"),
            block: "首页",
            blockvalue: "",
            version: app.globalData.version,
        };
        util.request('service.php', bannerData, ).then(res => {
            this.setData({
                bannerArray: res.data.data,
            })
        })
    },




    // 其他频道内容
    getInfoData() {
        let infoData = {
            citycode: this.data.citycode,
            action: 'indexlist',
            pagesize: 4,
            version: app.globalData.version,
        };
        util.request('index.php', infoData, 'GET').then(res => {
            // let infores = res.data.filter(v => v.wapname != "办事");
            let infores = res.data;
            this.setData({
                infolist: infores,
            })

            this.setData({
                showloading: false,
            })
        })
    },

    // 问答接口
    getwendaData() {
        let infoData = {
            citycode: "wenda",
            action: 'list',
            pagesize: 4,
            siteid: 46,
            city: wx.getStorageSync("city"),
            version: app.globalData.version,
        };
        util.request('index.php', infoData, 'GET').then(res => {
            this.setData({
                wendalist: res.data,
            })
        })
    },

    // 页面点击事件
    indexBtn() {
        wx.setStorageSync("indexCue", "indexCue")
        this.setData({
            indexCue: false,
        })
    },

    // 栏目点击事件
    lanmuTap(e) {
        let id = e.currentTarget.dataset.id;
        let type = e.currentTarget.dataset.type;
        // let name = e.currentTarget.dataset.name;
        wx.navigateTo({
            url: '../article/list/list?id=' + id + '&type=' + type + '&city=' + this.data.citycode,
        })

    },


    // banner点击事件
    bannerClick(e) {
        let path = e.currentTarget.dataset.path;
        let linkname = e.currentTarget.dataset.title;
        let appid = e.currentTarget.dataset.appid;
        if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1 && !appid) {
            this.setData({
                showtk: true
            })
            util.navContact(linkname, path)
        } else {
            util.navMethod(path, appid);
        }
    },

    // 工具、服务点击事件
    servBtn(e) {
        let index = e.currentTarget.dataset.index;
        let stitle = this.data.hotbsyData[index].title;
        let path = e.currentTarget.dataset.path;
        let linkname = e.currentTarget.dataset.title;
        let appid = e.currentTarget.dataset.appid;
        if (linkname === "全部") {
            wx.navigateTo({
                url: "/pages/tools/allServices/allServices",
            })
        } else {
            if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1) {
                this.setData({
                    showtk: true
                })
                util.navContact(linkname, path)
            } else {
                let scene = util.getScene(wx.getLaunchOptionsSync().scene);
                util.navMethod(path, appid, stitle, scene);
            }
        }
    },

    // 进入客服会话
    contact(e) {
        this.setData({
            showtk: false,
        })
    },

    // 关闭
    close() {
        this.setData({
            showtk: false,
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: "本地宝",
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg",
            path: "/pages/index/index"
        }
    },

    onShareTimeline: function () {
        return {
            title: "本地宝",
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg"
        }
    },

    /**
     * 页面滑动事件的处理函数
     */
    onPageScroll: function (e) {
        if (!wx.getStorageSync("indexCue")) {
            wx.setStorageSync("indexCue", "indexCue")
        }
        this.setData({
            indexCue: false,
            scrollTop: e.scrollTop,
        })
    }

})